﻿// Nail framework
//
// Copyright © Frank Nagl, 20010
// admin@franknagl.de
// www.franknagl.de
//
namespace Nail
{
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// Helper class to extract the Window Class Name by using user32.dll.
    /// </summary>
    public static class ClassName
    {
        [DllImport("user32.dll")]
        private static extern int GetClassName(
            int hwnd,
            [MarshalAs(
                UnmanagedType.LPStr)] 
                StringBuilder buf,
            int nMaxCount);

        /// <summary>
        /// Gets the name of the passed windows <paramref name="control"/>.
        /// </summary>
        /// <param name="control">The windows control from which the name will 
        /// be extracted.</param>
        /// <returns>Name of the passed windows <paramref name="control"/>.</returns>
        public static string Get(Control control)
        {
            int capacity = 256;
            string s;
            StringBuilder sb = new StringBuilder(capacity);
            GetClassName(control.Handle.ToInt32(), sb, capacity);
            s = sb.ToString();
            return s;
        }
    }
}
